<?php
namespace App\Controller;
//namespace App\Service\ProfileService;
use App\Service\LikeService;
use App\Service\UserService;
use Swoole;
use App\Service\MatchService;
use App\DAO;
class match extends Base
{

    public $userinfo;
    function __construct(\Swoole $swoole)
    {

        //Swoole::getInstance()->beforeAction($this->checkhead());

        parent::__construct($swoole);

        //self::checkhead();
    }
    function online()
    {

        //$this->checkhead();
        $matchService = new MatchService();
        $ret = $matchService->online($this->userinfo);
        if($ret)
        {
            $ret = array(
                "errorcode" =>'0',
                'errmsg'=>"成功",
            );
        }
        else
        {
            $ret = array(
                "errorcode" =>'1',
                'errmsg'=>"失败",
            );
        }
        return json_encode($ret);

    }
    function offline()
    {

        //$this->checkhead();
        $matchService = new MatchService();
        $ret = $matchService->offline($this->userinfo);
        if($ret)
        {
            $ret = array(
                "errorcode" =>'1',
                'errmsg'=>"成功",
            );
        }
        else
        {
            $ret = array(
                "errorcode" =>'0',
                'errmsg'=>"失败",
            );
        }
        return json_encode($ret);
    }
    function start()
    {
        $userSlect = $_GET['sex'];
        $matchService = new MatchService();
        $ret = $matchService->match($this->userinfo,$userSlect);
        if($ret['errorcode'] == 0)
        {
            $userService  = new UserService();

            $userinfo  = $userService->getUserInfobyUserId($ret['callinfo']['peerid']);
            $ret['userinfo'] = $userinfo;
        }
//        $ret = array(
//            "errorcode" =>'1',
//            'errmsg'=>"匹配成功",
//            'callinfo'=>$callinfo
//        );
        return json_encode($ret);
    }
    function end()
    {
        $callid = $_POST['callid'];
        $status = $_POST['status'];
        $matchService = new MatchService();
        $callinfo = $matchService->updatecallinfo($this->userinfo['user_id'],$callid,$status);
        $ret = array(
            "errorcode" =>'0',
            'errmsg'=>"匹配结束",

        );
        return json_encode($ret);
    }
    function addtime()
    {
        $callid = $_GET['callid'];

        $matchService = new MatchService();

        $callinfo =  $matchService->addtime($callid,$this->userinfo['user_id'],'60');
        $ret = array(
            "errorcode" =>'0',
            'errmsg'=>"加时成功",
            'callinfo'=>$callinfo
        );
        return json_encode($ret);
    }

    function like()
    {
        $callid = $_GET['callid'];
        $matchService = new MatchService();

        $callinfo =  $matchService->like($callid,$this->userinfo['user_id']);

        $likeService = new LikeService();
        $likeService->like($this->userinfo['user_id'],$callinfo['peerid']);
        $ret = array(
            "errorcode" =>'0',
            'errmsg'=>"成功",

        );
        return json_encode($ret);

    }

    function  get()
    {
        $callid = $_GET['callid'];
        $matchService = new MatchService();
        $userService = new UserService();
        $callinfo = $matchService->getcallinfo($callid);
        if($this->userinfo['userid'] == $callinfo['userid'])
        {
            $peerid = $callinfo['peerid'];
            $userinfo = $userService->getUserInfobyUserId($peerid);
        }
        $ret = array(
            "errorcode" =>'0',
            'errmsg'=>"获取成功",
            'callinfo'=>$callinfo,
            'userinfo' =>$userinfo
        );
        return json_encode($ret);
    }

}